/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.middleware.quartz.job.djtax;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.entity.HyTaxSalesinvoice;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.service.IHyTaxSalesinvoiceService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 税务上报销项票
 *
 * @author duanlikao
 * @created 2021/5/13
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SalesinvoiceUploadJob implements Job {
    private static final Logger LOGGER = LoggerFactory.getLogger(SalesinvoiceUploadJob.class);



    @Autowired
    private IHyTaxSalesinvoiceService taxSalesinvoiceService;


    public SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    //@Scheduled(cron = "0 01 2 * * ?")//每天的02:01:00执行 临时注释
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getDongjiangIzUse()){
            LOGGER.info("天津东疆税务上报服务暂未开启,请联系管理员。" );
            return;
        }

        Long start = System.currentTimeMillis();

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -2);// 把日期往后增加一天.整数往后推,负数往前移动
        Date lasDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        String startDateString =  DateUtils.date2Str(lasDate, DateUtils.date_sdf.get());
        String endDateString =  DateUtils.date2Str(new Date(), DateUtils.date_sdf.get());


        QueryWrapper<HyTaxSalesinvoice> queryWrapper = new QueryWrapper();

        //String  yearStartTime =  DateUtils.formatDate1(DateUtils.getYearStartTime(start));
        //queryWrapper.ge("kpsj", yearStartTime);
        //queryWrapper.between("kpsj",startDateString,endDateString);
        queryWrapper.ne("upload_status","1");
        queryWrapper.groupBy("fp_id");
        List<HyTaxSalesinvoice> taxSalesinvoices =  taxSalesinvoiceService.list(queryWrapper);
        if(CollectionUtils.isEmpty(taxSalesinvoices)) {
            LOGGER.info("UploadJobService uploadSalesinvoic: 没有数据处理 !" );
            return;
        }

        LOGGER.info("UploadJobService uploadSalesinvoice:执行税务上报销项票数据开始" + now.format(new Date()));

        for(HyTaxSalesinvoice taxSalesinvoice:taxSalesinvoices){
            try {
                taxSalesinvoiceService.uploadTax(taxSalesinvoice.getFpId());
            }catch (Exception e){
                LOGGER.info("UploadJobService uploadSalesinvoice:Exception : {}" ,e.getMessage());
                continue;
            }
        }

        LOGGER.info("UploadJobService uploadSalesinvoice:执行税务上报销项票数据结束,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");

    }



}
